package com.vercon.moensmart.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.vercon.moensmart.adapter.base.BaseAdapter
import com.vercon.moensmart.databinding.ItemHealthDetailsTabLayoutBinding
import com.vercon.moensmart.mode.HealthMeasureTarget

class HealthDetailsTabAdapter(context: Context?) : BaseAdapter<ItemHealthDetailsTabLayoutBinding, HealthMeasureTarget?>(context) {

    private var index = 0

    fun setCurrentIndex(index: Int) {
        this.index = index
        notifyDataSetChanged()
    }

    fun setCurrentTab(tab : String){
        for ((i,bean) in list.withIndex()){
            if(bean?.type.equals(tab)){
                this.index = i
                notifyDataSetChanged()
            }
        }
    }

    override fun baseViewHolder(parent: ViewGroup): ViewHolder {
        val binding: ItemHealthDetailsTabLayoutBinding = ItemHealthDetailsTabLayoutBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding)
    }

    override fun bindItemView(viewHolder: BaseViewHolder?, position: Int, t: HealthMeasureTarget?, total: Int) {
        viewHolder?.mBinding?.tvTab?.text = t?.getTitle(context)

        if(index == position){
            context?.resources?.getColor(com.vercon.common.R.color.color_4D9DB4)?.let { viewHolder?.mBinding?.tvTab?.setTextColor(it) }
            viewHolder?.mBinding?.vSelect?.visibility = View.VISIBLE
        }else{
            context?.resources?.getColor(com.vercon.common.R.color.color_99B5BC)?.let { viewHolder?.mBinding?.tvTab?.setTextColor(it) }
            viewHolder?.mBinding?.vSelect?.visibility =  View.GONE
        }

        viewHolder?.itemView?.setOnClickListener {
            setCurrentIndex(position)
            mItemListener?.onItemClickListener(it,t,position)
        }
    }
    inner class ViewHolder(itemBinding: ItemHealthDetailsTabLayoutBinding) : BaseViewHolder(itemBinding)
}